package com.foxconn.ipebg.common.system.controller;

import ipebg.mzk.facade.entity.OrgLevel;
import ipebg.mzk.facade.system.service.OrgLevelService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/orgLevel.do")
public class OrgLevelController {

	private Integer rows = 10;
	private Integer page = 1;

	@Resource(name = "orgLevelImpl")
	OrgLevelService orgLevelImpl;

	/**
	 * 加載層級頁面方法
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(params = "action=list")
	public String loadRoleInfo(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		return "/pages/common/system/levelInfo";
	}

	/**
	 * 查詢層級資料清單
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(params = "action=levelList")
	public void roleList(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		/*try {
			int from = Integer.parseInt(request.getParameter("page"));// 從前臺獲取分頁信息
			int length = Integer.parseInt(request.getParameter("rows"));
			String levelName = request.getParameter("levelName");
			OrgLevel org_1 = new OrgLevel();
			org_1.setLevelName(levelName);
			Page initiontpage = orgLevelImpl.getOrgLevelList(levelName, null,
					null, from, length);// getOrgLevelList(org_1,from,length);
			List<OrgLevel> OrgLevelList = (List<OrgLevel>) initiontpage
					.getResult();
			SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
			List<OrgLevelBean> orgLevelBeanList = new ArrayList<OrgLevelBean>();
			for (OrgLevel org_2 : OrgLevelList) {
				OrgLevelBean org = new OrgLevelBean();
				org.setLevelID(org_2.getLevelId());
				org.setCreateName(orgLevelImpl.findNameByNumber(org_2
						.getCreateName()));
				org.setLevelName(org_2.getLevelName());
				org.setLevelNumber(org_2.getLevelNumber().toString());
				String createdTime = null;
				if (org_2.getCreateTime() == null) {
					createdTime = null;
				} else {
					createdTime = date.format(org_2.getCreateTime());
				}
				org.setCreateTime(createdTime);
				orgLevelBeanList.add(org);
			}

			Map objMap = new HashMap();
			objMap.put("rows", orgLevelBeanList);
			objMap.put("total", initiontpage.getTotalCount());
			response.getWriter().write(JSON.toJSONString(objMap));
		} catch (Exception e) {
			e.printStackTrace();
		}*/
	}

	/**
	 * 新增層級資料
	 * 
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(params = "action=orgLevelAdd")
	public void roleAdd(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		/*String roleCode = request.getParameter("name");
		String roleName = request.getParameter("level");
		OrgLevel org = orgLevelImpl.findOrgLevelByNumber(Integer
				.parseInt(roleName));
		OrgLevel org1 = orgLevelImpl.findOrgLevelByName(roleCode);
		if (org != null || org1 != null) {
			try {
				response.getWriter().write("LEVEL");
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				return;
			}
		} else {
			OrgLevel roleBean = new OrgLevel();
			roleBean.setLevelName(roleCode);
			roleBean.setLevelNumber(Integer.parseInt(roleName));
			roleBean.setCreateTime(new Date());
			roleBean.setCreateName(request.getSession()
					.getAttribute("username").toString());
			try {
				orgLevelImpl.saveOrUpdate(roleBean);

				response.getWriter().write("SUCCESS");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}*/
	}

	/**
	 * 修改層級資料
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @throws Exception
	 */
	@RequestMapping(params = "action=orgLevelEdit")
	public void roleEdit(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		String roleId = request.getParameter("levelID");
		String roleCode = request.getParameter("levelName");
		String roleName = request.getParameter("levelNumber");
		OrgLevel org = orgLevelImpl.findOrgLevelByNumber(Integer
				.parseInt(roleName));
		OrgLevel org1 = orgLevelImpl.findOrgLevelByName(roleCode);
		if (org != null || org1 != null) {
			if (org.getLevelId().equals(roleId)
					&& org1.getLevelId().equals(roleId)) {
				response.getWriter().write("SUCCESS");
			} else {
				response.getWriter().write("LEVEL");
			}
		} else {
			OrgLevel orgLevel = orgLevelImpl.findOrgLevelById(roleId);
			orgLevel.setLevelName(roleCode);
			orgLevel.setLevelNumber(Integer.parseInt(roleName));
			//orgLevelImpl.save(orgLevel);
			response.getWriter().write("SUCCESS");
		}

	}

	/**
	 * 刪除層級資料
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @throws Exception
	 */
	@RequestMapping(params = "action=deleteOrgLevel")
	public void deleteRole(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		String orgLevelId = request.getParameter("roleId");
		boolean flag = orgLevelImpl.isUsed(orgLevelId);
		if (flag == true) {
			response.getWriter().write("USED");
		} else if (orgLevelImpl.deleteOrgLevel(orgLevelId)) {
			response.getWriter().write("SUCCESS");
		} else {
			response.getWriter().write("ERROR");
		}
	}

}
